package com.yilin.tms.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yilin.tms.core.commons.model.AuthStatus;
import com.yilin.tms.core.entity.data.statistics.UserNoAuthStatistics;
import com.yilin.tms.core.entity.data.statistics.user.*;
import com.yilin.tms.core.entity.user.OrganType;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.entity.user.auth.*;
import com.yilin.tms.user.mapper.auth.*;
import com.yilin.tms.user.service.IStatisticsService;
import com.yilin.tms.user.mapper.UserStatisticsMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class StatisticsService implements IStatisticsService {
    @Resource
    private UserStatisticsMapper countMapper;
    @Resource
    BusinessLicenseMapper businessLicenseMapper;
    @Resource
    DrivingLicenseMapper drivingLicenseMapper;
    @Resource
    IdCardMapper idCardMapper;
    @Resource
    QualificationCertMapper qualificationCertMapper;
    @Resource
    TransBusinessLicenseMapper transBusinessLicenseMapper;
    @Resource
    StampPersonalSignetMapper stampPersonalSignetMapper;
    @Resource
    StampCompanySealMapper stampCompanySealMapper;
    @Resource
    TaxCertificationMapper taxCertificationMapper;

    /**
     * 获取用户统计数据
     */
    @Override
    public UserStatistics getUserStatistics(Account loginUser) {
        //用户总数,用户本月新增计量,用户月新增计量,用户日新增计量
        UserStatistics userStatistics = countMapper.getUserCount(loginUser);
        //用户类型类型饼图
        userStatistics.setUserTypeCake(countMapper.getUserTypeCake(loginUser));
        //角色分布饼图
        userStatistics.setUserRoleCake(countMapper.getUserRoleCake(loginUser));
        //用户活跃趋势折线
        userStatistics.setUserActiveTrends(countMapper.getUserActiveTrends(loginUser));
        //用户注册趋势折线
        userStatistics.setUserRegistrationTrends(countMapper.getUserRegistrationTrends(loginUser));
        return userStatistics;
    }

    /**
     * 获取孵化统计数据
     */
    @Override
    public HatchStatistics getHatchStatistics(Account loginUser) {
        //总孵化数,年孵化数,月孵化数
        HatchStatistics hatchStatistics = countMapper.getHatchCount(loginUser);
        //孵化趋势
        hatchStatistics.setHatchTrends(countMapper.getHatchTrends(loginUser));
        return hatchStatistics;
    }

    /**
     * 获取货主统计数据
     */
    @Override
    public ShipperStatistics getShipperStatistics(Account loginUser) {
        //货主总数，月新增数，日新增数
        ShipperStatistics shipperStatistics = countMapper.getShipperCount(loginUser);
        //货主类型分布饼图
        shipperStatistics.setShipperTypeCake(countMapper.getOrganTypeCake(loginUser, UserType.shipperUser));
        //货主角色分布饼图
        shipperStatistics.setShipperRoleCake(countMapper.getShipperOrTruckerRoleCake(loginUser, UserType.shipperUser));
        //货主新增趋势
        shipperStatistics.setShipperRegisterTrends(countMapper.getShipperRegisterTrends(loginUser));
        //货主活跃趋势
        shipperStatistics.setShipperActiveTrends(countMapper.getShipperActiveTrends(loginUser));
        return shipperStatistics;
    }

    /**
     * 获取车主统计数据
     */
    @Override
    public TruckerStatistics getTruckerStatistics(Account loginUser) {
        //车主总数，月新增数，日新增数
        TruckerStatistics truckerStatistics = countMapper.getTruckerCount(loginUser);
        //车主类型分布饼图
        truckerStatistics.setTruckerTypeCake(countMapper.getOrganTypeCake(loginUser, UserType.truckerUser));
        //车主角色分布饼图
        truckerStatistics.setTruckerRoleCake(countMapper.getShipperOrTruckerRoleCake(loginUser, UserType.truckerUser));
        //车主新增趋势
        truckerStatistics.setTruckerRegisterTrends(countMapper.getTruckerRegisterTrends(loginUser));
        //车主活跃趋势
        truckerStatistics.setTruckerActiveTrends(countMapper.getTruckerActiveTrends(loginUser));
        return truckerStatistics;
    }

    /**
     * 查找：用户证件未认证统计
     */
    @Override
    public UserNoAuthStatistics getUserNoAuthStatistics() {
        UserNoAuthStatistics statistics = new UserNoAuthStatistics();
        statistics.setIdCard(Math.toIntExact(idCardMapper.selectCount(new QueryWrapper<IdCard>().eq("auth_status", AuthStatus.waitProcess).eq("is_deleted", 0))));
        statistics.setBusinessLicense(Math.toIntExact(businessLicenseMapper.selectCount(new QueryWrapper<BusinessLicense>().eq("auth_status", AuthStatus.waitProcess).eq("is_deleted", 0))));
        statistics.setDrivingLicense(Math.toIntExact(drivingLicenseMapper.selectCount(new QueryWrapper<DrivingLicense>().eq("auth_status", AuthStatus.waitProcess).eq("is_deleted", 0))));
        statistics.setQualificationCert(Math.toIntExact(qualificationCertMapper.selectCount(new QueryWrapper<QualificationCert>().eq("auth_status", AuthStatus.waitProcess).eq("is_deleted", 0))));
        statistics.setStampCompanySeal(Math.toIntExact(stampCompanySealMapper.selectCount(new QueryWrapper<StampCompanySeal>().eq("auth_status", AuthStatus.waitProcess).eq("is_deleted", 0))));
        statistics.setStampPersonalSignet(Math.toIntExact(stampPersonalSignetMapper.selectCount(new QueryWrapper<StampPersonalSignet>().eq("auth_status", AuthStatus.waitProcess).eq("is_deleted", 0))));
        statistics.setTaxCertification(Math.toIntExact(taxCertificationMapper.selectCount(new QueryWrapper<TaxCertification>().eq("auth_status", AuthStatus.waitProcess).eq("is_deleted", 0))));
        statistics.setTransBusinessLicense(Math.toIntExact(transBusinessLicenseMapper.selectCount(new QueryWrapper<TransBusinessLicense>().eq("auth_status", AuthStatus.waitProcess).eq("is_deleted", 0))));
        if (statistics.getIdCard() == 0 && statistics.getBusinessLicense() == 0 && statistics.getDrivingLicense() == 0 && statistics.getQualificationCert() == 0 && statistics.getStampPersonalSignet() == 0
                && statistics.getStampCompanySeal() == 0 && statistics.getTaxCertification() == 0 && statistics.getTransBusinessLicense() == 0) {
            statistics.setAllEmpty(true);
        }
        return statistics;
    }

    @Override
    public Map<String, Object> getCreditAssetsData(String userId, String organizeId) {

        return countMapper.getCreditAssetsData(userId, organizeId);
    }
}
